package com.hsy.order.common.service

import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.IBinder
import com.flux.utils.LogUtil
import com.hsy.order.R
import com.hsy.order.common.ui.MainActivity

/**
 * Created on 2018/5/23.
 *
 * @author Yang
 */
class OrderService : Service() {

    override fun onCreate() {
        super.onCreate()
        LogUtil.i("OrderService", "onCreate()")
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        LogUtil.i("OrderService", "onStartCommand()")

        // 在API11之后构建Notification的方式
        val builder = Notification.Builder(this.applicationContext) //获取一个Notification构造器
        val nfIntent = Intent(this, MainActivity::class.java)

        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher)) // 设置下拉列表中的图标(大图标)
                .setContentTitle("您正在使用接单助手接单") // 设置下拉列表里的标题
                .setSmallIcon(R.drawable.ic_launcher) // 设置状态栏内的小图标
                .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间

        val notification = builder.build() // 获取构建好的Notification
        startForeground(101, notification)
        return START_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onDestroy() {
        LogUtil.i("OrderService", "onDestroy()")
        stopForeground(true)
        super.onDestroy()
    }
}